using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShivamEMR.Model.Domain.PersonDomain;
using ShivamEMR.Model.DataInterfaces.PersonDataInterf;
using ShivamEMR.Model;
using ShivamEMR.Web.Models;
//using ShivamEMR.Web.Areas.Person.Models;

namespace ShivamEMR.Web.Areas.Person.Controllers
{   
    public class PersonRoleController : Controller
    {
		private readonly IPersonRoleRepository personroleRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public PersonRoleController() 
        {
            personroleRepository = Registry.RepositoryFactory.GetPersonRoleRepository();
        }

        public PersonRoleController(IPersonRoleRepository personroleRepository)
        {
			this.personroleRepository = personroleRepository;
        }

        //
        // GET: /PersonRole/
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ViewResult Index()
        {
           // return View(personroleRepository.AllIncluding(personrole => personrole.Persons));
            return View(personroleRepository.All);
        }

        //
        // GET: /PersonRole/Details/5
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ViewResult Details(int id)
        {
            return View(personroleRepository.Find(id));
        }

        //
        // GET: /PersonRole/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /PersonRole/Create
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        [HttpPost]
        public ActionResult Create(PersonRole personrole)
        {
            if (ModelState.IsValid) {
                personroleRepository.Create(personrole);
                personroleRepository.Save();
                return RedirectToAction("Index");
            } else {
				return View();
			}
        }
        
        //
        // GET: /PersonRole/Edit/5
 [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ActionResult Edit(int id)
        {
             return View(personroleRepository.Find(id));
        }

        //
        // POST: /PersonRole/Edit/5
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        [HttpPost]
        public ActionResult Edit(PersonRole personrole)
        {
            if (ModelState.IsValid) {
                personroleRepository.Update(personrole);
                personroleRepository.Save();
                return RedirectToAction("Index");
            } else {
				return View();
			}
        }

        //
        // GET: /PersonRole/Delete/5
 [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ActionResult Delete(int id)
        {
            return View(personroleRepository.Find(id));
        }

        //
        // POST: /PersonRole/Delete/5
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
           // personroleRepository.Delete(id);
            var prole = personroleRepository.Find(id);
            personroleRepository.Delete(prole);
            personroleRepository.Save();

            return RedirectToAction("Index");
        }
    }
}

